<?php

class GL_Item extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->layout->setLayout("home/layout");
    }

    public function index($pid)
    {

        $session = $this->session->userdata('logged_in');
        $uid = $session['id'];

        $purchased = $this->purchasing->purchased($uid,$pid);

        $data['purchased'] = $purchased;

        $promo = $this->product->get_promotion($pid);



        if ($pid==null) $pid = 1;
        $product = $this->product->get($pid)[0];
        $sameAuthors = $this->product->get_all_product(1,0,$product->uid,'',0);
        $sameCats = $this->product->get_all_product(1,$product->cid,0,'',0);

        $data['images'] = $this->product->get_images($pid);
        $data['uid'] = $uid;
        $data['product'] = $product;
        $data['tags'] = $this->product->get_tags($pid);
        $data['sameAuthors'] = $sameAuthors;
        $data['sameCats'] = $sameCats;
        for ($i = 0; $i < count($sameAuthors); $i++) {
            if ($sameAuthors[$i]->id!=$product->id)
                $data['images_s'][$i] = $this->product->get_images($sameAuthors[$i]->id)[0];
            else  $data['images_s'][$i] =null;
        }
        for ($i = 0; $i < count($sameCats); $i++) {
            if ($sameCats[$i]->id!=$product->id)
                $data['images_sc'][$i] = $this->product->get_images($sameCats[$i]->id)[0];
            else  $data['images_sc'][$i] =null;
        }

        $data['promo'] = $promo;

        if ($promo&&$promo->start<=time()&&$promo->end>time()) {
            $data['sale'] = round($product->price - $promo->discount/100*$product->price,1);
        }
        $year = date('Y');
        $data['downloads'] = "";
        for ($i = 1; $i < 13; $i++) {
            $data['downloads'] .= $this->product->count_downloads($pid, $i, $year)['count(*)'] . ",";
        }

        $data['downloads'] = substr($data['downloads'], 0, strlen($data['downloads']) - 1);
        $this->layout->view("home/item",$data);
    }

    public function add_cart(){
        $pid = $this->input->post('pid');
        echo $this->purchasing->add_cart($pid);
    }

    public function download($id){
        //Kiểm tra user đã mua item chưa
        $session = $this->session->userdata('logged_in');
        $uid = $session['id'];
        if ($this->purchasing->check_user_purchased_product($uid,$id))
        {
            $attachment_location = $_SERVER["DOCUMENT_ROOT"]."/GraphicLand/ProductFile/" . $id.'.zip';
            if (file_exists($attachment_location)) {
                header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
                header("Cache-Control: public"); // needed for i.e.
                header("Content-Type: application/zip");
                header("Content-Transfer-Encoding: Binary");
                header("Content-Length:".filesize($attachment_location));
                header("Content-Disposition: attachment; filename=graphicland-".$id.".zip");
                readfile($attachment_location);
                die();
            } else {
                $attachment_location = $_SERVER["DOCUMENT_ROOT"]."/GraphicLand/ProductFile/download.zip";
                if (file_exists($attachment_location)) {
                    header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
                    header("Cache-Control: public"); // needed for i.e.
                    header("Content-Type: application/zip");
                    header("Content-Transfer-Encoding: Binary");
                    header("Content-Length:" . filesize($attachment_location));
                    header("Content-Disposition: attachment; filename=graphicland-" . $id . ".zip");
                    readfile($attachment_location);
                    die();
                }
            }
        }
    }

    public function create_promotion(){
        $pid = $this->input->post('pid');
        $start = $this->input->post('start');
        $end = $this->input->post('end');
        $discount = $this->input->post('discount');

        echo $this->product->create_promotion($pid,$start,$end,$discount);
    }

    public function delete_promotion(){
        $pid = $this->input->post('pid');
        echo $this->product->delete_promotion($pid);
    }
}